﻿using System;
using System.Web;
using System.Data;
using System.Linq;
using System.Web.UI;
using System.Data.SqlClient;
using ColorApplication.Helper;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Configuration;


namespace ColorApplication.color
{
    public partial class Settings : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Checking whether Admin or Not. AdminID key is in web.config
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "" && Request.QueryString["id"] == ConfigurationManager.AppSettings["AdminID"].ToString())
            {
                Clear();
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
            if (!IsPostBack)
            {
                //double ColorPercentage = 0.0;
                //double resultColorPercentage = 0.0;

                //if (Session["csvdatatable"] != null)
                //{
                //    if (Session["Colordatatable"] != null)
                //    {
                //        DataTable colorDataTable = (DataTable)Session["Colordatatable"];

                //        foreach (DataRow row in colorDataTable.Rows)
                //        {
                //            if (row["Color"].ToString() == "Red")
                //            {
                //                ColorPercentage = Convert.ToDouble(row["Percentage"].ToString());
                //            }
                //        }
                //    }
                //}

                //if (ColorPercentage != 0.0)
                //{
                //    ColorPercentagetextBox.Text = Convert.ToString(ColorPercentage);

                //    if (ColorPercentage > 19.099)
                //    {
                //        resultColorPercentage = ColorPercentage * 0.05;
                //        ResultPercentagetextBox.Text = Convert.ToString(resultColorPercentage);
                //    }
                //    else
                //    {
                //        ResultPercentagetextBox.Text = Convert.ToString(ColorPercentage);
                //    }
                //}
                //else
                //{
                //    ColorPercentagetextBox.Text = "0.0";
                //    ResultPercentagetextBox.Text = "0.0";
                //}
                DataTable dt = getSettingTable();
                if (dt != null)
                {
                    ColorPercentagetextBox.Text = dt.Rows[0]["ColorPercentage"].ToString();
                    ResultPercentagetextBox.Text = dt.Rows[0]["ResultPercentage"].ToString();
                    DominantAttributesFactortextBox.Text = dt.Rows[0]["DominantAttributesFactor"].ToString();
                    ddlUseOffset.SelectedValue = dt.Rows[0]["UseOffset"].ToString();
                    txtCommonMessage.Text = Convert.ToString(dt.Rows[0]["CommonMessage"]);
                    MaximumAttributeFamiliesToInclude.Text = Convert.ToString(dt.Rows[0]["MaximumAttributeFamilies"]);
                    MinimumAttributeFamilyPercentage.Text = Convert.ToString(dt.Rows[0]["MinimumPercentage"]);
                    if (dt.Rows[0]["GoViaPayment"] != DBNull.Value)
                        chkGoViaPayment.Checked = Convert.ToBoolean(dt.Rows[0]["GoViaPayment"]);
                    txtPrice.Text = dt.Rows[0]["Price"].ToString();
                }


            }
        }

        public void Clear()
        {
            // Clear Error And Success label Every Post Bak
            SuccesLabel.Text = "";
            ErrorLabel.Text = "";

        }

        protected void SaveBtton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(ColorPercentagetextBox.Text) && !string.IsNullOrWhiteSpace(ResultPercentagetextBox.Text))
            {
                decimal colorPercentage = Convert.ToDecimal(ColorPercentagetextBox.Text.Trim());
                decimal resultPercentage = Convert.ToDecimal(ResultPercentagetextBox.Text.Trim());
                decimal DominantAttributesFactor = Convert.ToDecimal(DominantAttributesFactortextBox.Text.Trim());
                bool UseOffset = Convert.ToBoolean(ddlUseOffset.SelectedValue);
                string CommonMessage = txtCommonMessage.Text;
                int? MaxAttributes = null;
                if (!string.IsNullOrEmpty(MaximumAttributeFamiliesToInclude.Text))
                    MaxAttributes = Convert.ToInt32(MaximumAttributeFamiliesToInclude.Text);
                decimal? MinAttributeFamilyPercentage = null;
                if (!string.IsNullOrEmpty(MinimumAttributeFamilyPercentage.Text))
                    MinAttributeFamilyPercentage = Convert.ToDecimal(MinimumAttributeFamilyPercentage.Text);
                bool GoViaPayment = chkGoViaPayment.Checked;
                decimal Price = 0;
                if (!string.IsNullOrEmpty(txtPrice.Text.Trim()))
                    Price = Convert.ToDecimal(txtPrice.Text.Trim());

                if (isFirst())
                {
                    insertWhiteColorNeutralizationInsert(colorPercentage, resultPercentage, DominantAttributesFactor, UseOffset, CommonMessage, MaxAttributes, MinAttributeFamilyPercentage, Price, GoViaPayment);
                    SuccesLabel.Text = "Record inserted successfully.";
                }
                else
                {
                    UpdateWhiteColorNeutralization(colorPercentage, resultPercentage, DominantAttributesFactor, UseOffset, CommonMessage, MaxAttributes, MinAttributeFamilyPercentage, Price, GoViaPayment);
                    SuccesLabel.Text = "Record updated successfully.";
                }
            }
        }

        private void UpdateWhiteColorNeutralization(decimal colorPercentage, decimal resultPercentage, decimal DominantAttributesFactor, bool UseOffset, string CommonMessage, int? MaxAttributes, decimal? MinAttributeFamilyPercentage, decimal Price, bool GoViaPayment)
        {
            using (SqlConnection con = new SqlConnection())
            {
                int id = getId();
                SqlParameter[] parameters ={
                                          new SqlParameter("@ColorPercentage",colorPercentage),
                                          new SqlParameter("@ResultPercentage",resultPercentage),
                                          new SqlParameter("@DominantAttributesFactor",DominantAttributesFactor),
                                          new SqlParameter("@UseOffset",UseOffset),
                                          new SqlParameter("@CommonMessage",CommonMessage),
                                          new SqlParameter("@MaximumAttributeFamilies",MaxAttributes),
                                          new SqlParameter("@MinimumPercentage",MinAttributeFamilyPercentage),
                                          new SqlParameter("@GoViaPayment",GoViaPayment),
                                          new SqlParameter("@Price",Price),
                                          new SqlParameter("@Id",  id)
                                          };
                SqlCommand cmd = SQLHelper.GetCommand(con, "UpdateWhiteColorNeutralizationUpdate", parameters);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }

        private bool isFirst()
        {
            SqlDataReader dr;
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection())
            {

                SqlParameter[] parameter = { new SqlParameter("@Id", null) };
                SqlCommand cmd = SQLHelper.GetCommand(con, "GetWhiteColorNeutralizationSelect", parameter);
                con.Open();
                dr = cmd.ExecuteReader();
                dt.Load(dr);
                con.Close();
                if (dt != null && dt.Rows.Count > 0)
                    return false;
                return true;
            }
        }

        private int getId()
        {
            SqlDataReader dr;
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection())
            {

                SqlParameter[] parameter = { new SqlParameter("@Id", null) };
                SqlCommand cmd = SQLHelper.GetCommand(con, "GetWhiteColorNeutralizationSelect", parameter);
                con.Open();
                dr = cmd.ExecuteReader();
                dt.Load(dr);
                con.Close();
                if (dt != null && dt.Rows.Count > 0)
                {
                    return Convert.ToInt32(dt.Rows[0]["Id"]);
                }
                else
                {
                    return 0;
                }
            }
        }




        public void insertWhiteColorNeutralizationInsert(decimal colorPercentage, decimal resultPercentage, decimal DominantAttributesFactor, bool UseOffset, string CommonMessage, int? MaxAttributes, decimal? MinAttributeFamilyPercentage, decimal Price, bool GoViaPayment)
        {
            using (SqlConnection con = new SqlConnection())
            {
                SqlParameter[] parameters ={
                                          new SqlParameter("@ColorPercentage",colorPercentage),
                                          new SqlParameter("@ResultPercentage",resultPercentage),
                                          new SqlParameter("@DominantAttributesFactor",DominantAttributesFactor),
                                          new SqlParameter("@UseOffset",UseOffset),
                                          new SqlParameter("@CommonMessage",CommonMessage),
                                          new SqlParameter("@MaximumAttributeFamilies",MaxAttributes),
                                          new SqlParameter("@MinimumPercentage",MinAttributeFamilyPercentage),
                                          new SqlParameter("@Price",Price),
                                          new SqlParameter("@GoViaPayment",GoViaPayment)
                                         };
                SqlCommand cmd = SQLHelper.GetCommand(con, "InsertWhiteColorNeutralizationInsert", parameters);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }

        public DataTable getSettingTable()
        {
            SqlDataReader dr;
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection())
            {

                SqlParameter[] parameter = { new SqlParameter("@Id", null) };
                SqlCommand cmd = SQLHelper.GetCommand(con, "GetWhiteColorNeutralizationSelect", parameter);
                con.Open();
                dr = cmd.ExecuteReader();
                dt.Load(dr);
                con.Close();
                if (dt != null && dt.Rows.Count > 0)
                    return dt;
                return null;
            }
        }
    }
}